package com.astn.bks;

import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.astn.adapter.booking.DestinationAdapter;
import com.astn.adapter.booking.ProviderAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteTown;
import com.astn.entity.booking.QuoteUser;

public class CTopProvider extends Activity {

	// private ViewFlipper homeFlipper;
	private ListView lsvTop;
	private Button btnNext;
	private Button btnBack;
	private TextView txvPage;
	private ArrayList<QuoteUser> lstUser;// Top provider
	private Intent providerService;
	private int topProviderCurrentPage = 0;
	private int totalProviderPage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ctopprovider);
		lsvTop = (ListView) findViewById(R.id.lsvTop);
		btnNext = (Button) findViewById(R.id.btnNextPage);
		btnBack = (Button) findViewById(R.id.btnPreviousPage);
		txvPage = (TextView) findViewById(R.id.txvPage);
		getNumberPageProvider();
		txvPage.setText(String.valueOf(topProviderCurrentPage + 1) + "/"
				+ String.valueOf(totalProviderPage));
		reloadTopProvider();
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getNumberPageProvider();
				if (topProviderCurrentPage < totalProviderPage - 1) {
					topProviderCurrentPage++;
				} else {
					topProviderCurrentPage = 0;
				}
				txvPage.setText(String.valueOf(topProviderCurrentPage + 1)
						+ "/" + String.valueOf(totalProviderPage));
				reloadTopProvider();
			}

		});
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				getNumberPageProvider();
				if (topProviderCurrentPage > 0) {
					topProviderCurrentPage--;
				} else {
					topProviderCurrentPage = totalProviderPage - 1;
				}
				txvPage.setText(String.valueOf(topProviderCurrentPage + 1)
						+ "/" + String.valueOf(totalProviderPage));
				reloadTopProvider();
			}

		});

	}

	private void reloadTopProvider() {
		lstUser = new ArrayList<QuoteUser>();
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGE", topProviderCurrentPage);
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc", "getTopProvider",
					mapPara);
		} catch (Exception e) {

		}
		if (resultObj != null) {
			JSONArray arr = null;
			try {
				arr = (JSONArray) JSONValue.parse((String) resultObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (arr != null) {
				for (int i = 0; i < arr.size(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					try {
						QuoteUser tmpUser = new QuoteUser();
						tmpUser.setAddress((String) obj.get("address"));
						tmpUser.setEmail((String) obj.get("email"));
						tmpUser.setId(Integer.parseInt((String) obj.get("id")));
						tmpUser.setName((String) obj.get("name"));
						tmpUser.setPhone((String) obj.get("phone"));
						tmpUser.setType(Integer.parseInt((String) obj
								.get("type")));
						lstUser.add(tmpUser);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		}
		ProviderAdapter providerAdap = new ProviderAdapter(
				getApplicationContext(), lstUser);
		lsvTop.setAdapter(providerAdap);

	}

	private void getNumberPageProvider() {
		Object resultObj = null;
		Map<String, Object> mapPara = new HashMap<String, Object>();
		mapPara.put("PAGESIZE", BksConstants.PAGESIZE);
		try {
			resultObj = WebserviceUtils.call("com_xmlrpc",
					"getPageNumberTopProvider", mapPara);
			String temp = (String) resultObj;
			Log.d("D", temp);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (resultObj != null) {
			Float f = Float.parseFloat(resultObj.toString());
			totalProviderPage = f.intValue();
		} else {
			totalProviderPage = 0;
		}
	}


	// @Override
	// public boolean onTouch(View v, MotionEvent event) {
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// touchDown = event.getX();
	// break;
	// case MotionEvent.ACTION_UP:
	// touchUp = event.getX();
	// if (touchUp < touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_out_right));
	//
	// homeFlipper.showPrevious();
	// }
	// if (touchUp > touchDown) {
	// homeFlipper.setAnimation(AnimationUtils.loadAnimation(
	// Home.this, android.R.anim.slide_in_left));
	// homeFlipper.showNext();
	// }
	// break;
	//
	// default:
	// break;
	// }
	// return true;
	// }

}
